package service.impl;

import bean.Order;
import event.OrderSuccessEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import service.OrderService;

@Service
public class OrderServiceImpl implements OrderService, ApplicationEventPublisherAware {

    private ApplicationEventPublisher publisher;
    @Override
    public void buy(Order order) {
        System.out.println("下单成功..."+order);
        // 发布事件
        publisher.publishEvent(new OrderSuccessEvent(this, order));
    }

    /**
     * Set the ApplicationEventPublisher that this object runs in.
     * <p>Invoked after population of normal bean properties but before an init
     * callback like InitializingBean's afterPropertiesSet or a custom init-method.
     * Invoked before ApplicationContextAware's setApplicationContext.
     *
     * @param applicationEventPublisher event publisher to be used by this object
     */
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }
}
